import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'

const routes = [
  {
    path: '/',
    name: 'login',
    component: LoginView
  },
  {
    path: "/main",
    name: "main",
    component: () => import("../views/MainView.vue"),
    children: [
        {
          path: "/main",
          component: () => import("../views/UserView.vue")
        },
        {
          path: "/manager",
          component: () => import("../views/ManagerView.vue")
        },
        {
          path: "/parent",
          component: () => import("../views/ParentInfor.vue")
        },
        {
          path: "/district",
          component: () => import("../views/DistrictView.vue")
        }
    ]
  }
]

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  console.log(localStorage["token"]);
  let isAuthenticated = !!localStorage["token"];
  if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
  // 如果用户未能验证身份，则 `next` 会被调用两次
  else next()
})
export default router
